# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=re2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=2023-09-01
pkgver=${_pkgver//-/}
pkgrel=1
pkgdesc="Fast, safe, thread-friendly regular expression engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/google/re2"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(${_realname}-${_pkgver}.tar.gz::https://github.com/google/re2/archive/${_pkgver}.tar.gz
        001-fix-pkgconfig-file.patch)
sha256sums=('5bb6875ae1cd1e9fedde98018c346db7260655f86fdb8837e3075103acd3649b'
            '97d1aedfa763b162219d71144a8806e841c5e502048ad823e8bddc7fdad3dbcf')

prepare() {
  cd "${srcdir}"/${_realname}-${_pkgver}
  patch -p1 -i "${srcdir}"/001-fix-pkgconfig-file.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  echo "Build static for ${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DRE2_BUILD_TESTING=OFF \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./

  echo "Build shared for ${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DRE2_BUILD_TESTING=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_SHARED_LIBRARY_NAME_WITH_VERSION=ON \
    ../${_realname}-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

check() {
  echo "Tests static for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-static"
  ${MINGW_PREFIX}/bin/cmake -DRE2_BUILD_TESTING=ON ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest ./ || true

  echo "Tests shared for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-shared"
  ${MINGW_PREFIX}/bin/cmake -DRE2_BUILD_TESTING=ON ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  echo "Install static for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  echo "Install shared for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${_pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
